<!--
功能：数据列表解析页面
作者：李成
日期：2023年11月30
-->
<script setup lang="ts">
import { ref, onMounted, nextTick, reactive, toRaw } from "vue";
import type { pageModel } from "@/views/dataList/utils/types.ts";
import addICO from "@iconify-icons/ep/circle-check";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { saveDataFormd, getFormStructAndData } from "@/api/dataList";
import { message } from "@/utils/message";
import { getToken } from "@/utils/auth";
import { type PaginationProps } from "@pureadmin/table";
import { getModeInfo, getDataLists } from "@/api/dataList";
import router from "@/router";
const generateForm = ref();
const oneModel = ref();
const editData = ref();
const jsonData = ref();
const isSuccess = ref(false);
const uploadField = ref([]);
const appID = ref();
const editCode = ref();
const modeID = ref();
const props = withDefaults(defineProps<pageModel>(), {
  modeID: 0,
  rsID: 0,
  isSearch: false,
  tableName: "",
  isSuccess: false,
  isSheng: false,
  editCode: 0,
  paretCode: ""
});
const pagination = reactive<PaginationProps>({
  total: 0,
  pageSize: 10,
  currentPage: 1,
  background: true
});
//保存的参数
const form = reactive({
  // 左侧部门树的id
  deptId: 0,
  appName: "",
  isShow: "",
  onePage: pagination,
  sortName: "id",
  sort: false,
  filterArr: [],
  searchKey: "",
  editTable: ""
});
const saveData = reactive({
  rsID: "",
  tableName: ""
});
onMounted(async () => {
  appID.value = router.currentRoute.value.name;
  const ResultModeInfo = await getModeInfo({
    appID: appID.value,
    clientID: 1,
    subID: 0
  });

  if (ResultModeInfo.isSuccess) {
    oneModel.value = ResultModeInfo.result;
    form.deptId = appID.value;
    editCode.value = oneModel.value.editCode;
    modeID.value = oneModel.value.oneModel.id;
    form.appName = oneModel.value.oneModel.showTable;
    form.editTable = oneModel.value.oneModel.showTable;
  }

  const Resultss = await getDataLists(toRaw(form));
  if (Resultss.isSuccess) {
    saveData.rsID = JSON.parse(Resultss.data)[0].id;
  }
  const Result = await getFormStructAndData({
    modeID: modeID.value,
    rsID: saveData.rsID
  });
  isSuccess.value = ResultModeInfo.isSuccess;
  // saveData.rsID = Result.result.rsID;
  jsonData.value = JSON.parse(Result.result.formStruct);
  editData.value = JSON.parse(Result.result.formData);
  setImageFileToken(Result.result.formStruct);
  setUploadFileToken(Result.result.formStruct);
  nextTick(() => {
    generateForm.value.refresh();
    if (Result.result.hideFields != null) {
      generateForm.value.hide(Result.result.hideFields);
    }
    if (Result.result.enabledFields != null) {
      generateForm.value.disabled(Result.result.enabledFields, true);
    }

    //要对值列表是否为空进行判断
    if (editData.value[0] != null) {
      generateForm.value.setData(editData.value[0]);
    }
    //要进行判断是否是父子表数据关系。
    if (props.paretCode != "" && editData.value[0] == null) {
      generateForm.value.setData(JSON.parse(props.paretCode));
    }

    //获取Token
    uploadField.value.forEach(item => {
      generateForm.value.setOptions(item, {
        headers: [
          { key: "Authorization", value: "Bearer " + getToken().accessToken }
        ]
      });
      //console.log(item, getToken().accessToken);
    });
  });
});

//imgupload
const setImageFileToken = (str: string) => {
  const regex = /name":".*?"/;
  let start = 0;
  while (start != -1) {
    start = str.indexOf("imgupload");
    if (start == -1) {
      break;
    }
    str = str.substring(start + 10);
    const oneFields = regex.exec(str);
    uploadField.value.push(oneFields[0].substring(7, oneFields[0].length - 1));
  }
};

//fileupload
const setUploadFileToken = (str: string) => {
  const regex = /name":".*?"/;
  let start = 0;
  while (start != -1) {
    start = str.indexOf("fileupload");
    if (start == -1) {
      break;
    }
    str = str.substring(start + 10);
    const oneFields = regex.exec(str);
    uploadField.value.push(oneFields[0].substring(7, oneFields[0].length - 1));
  }
};
const saveJson = async () => {
  const myfalg = true;
  generateForm.value.validate().then(async () => {
    generateForm.value.getData().then(async data => {
      const Result = await saveDataFormd({
        rsID: saveData.rsID,
        jsonStr: JSON.stringify(data),
        tableName: form.editTable,
        isSheng: props.isSheng,
        editCode: editCode.value
      });

      if (Result.isSuccess) {
        message("保存成功！", { type: "success" });
        //emit("delModel", true);
        isSuccess.value = true;
        //return false;
      } else {
        message(Result.msg, { type: "error" });
        isSuccess.value = false;
      }
    });
  });
  return myfalg;
};

defineExpose({ saveJson, isSuccess });
</script>

<template>
  <div>
    <fm-generate-form :data="jsonData" ref="generateForm" />
    <el-button
      type="primary"
      :icon="useRenderIcon(addICO)"
      style="margin-top: 12px; margin-left: 10px"
      @click="saveJson"
      v-if="(editCode = 100)"
      >保存</el-button
    >
    <el-button class="reset-margin" link type="primary" v-if="(editCode = 0)">
      详情
    </el-button>
  </div>
</template>
